package y23.Dec.math;


import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;

/**
 * 实现齐次坐标矩阵（4*4）
 */
class HomogeneousMatrix {
    private double[][] matrix;

    public HomogeneousMatrix() {
        //实现一个四乘四的矩阵
        matrix = new double[4][4];
    }

    public double[][] getMatrix() {
        return matrix;
    }

    public void setMatrix(double[][] matrix) {
        this.matrix = matrix;
    }

    public static HomogeneousMatrix createRotationMatrix(double angle) {
        //实现旋转变换矩阵，其按照原点旋转
        HomogeneousMatrix matrix = new HomogeneousMatrix();
        double[][] m = matrix.getMatrix();

        double cos = Math.cos(angle);
        double sin = Math.sin(angle);

        m[0][0] = cos;
        m[0][1] = -sin;
        m[1][0] = sin;
        m[1][1] = cos;
        m[2][2] = 1;
        m[3][3] = 1;

        return matrix;
    }

    public static HomogeneousMatrix createScaleMatrix(double scaleX, double scaleY) {
        //实现缩放变换矩阵
        HomogeneousMatrix matrix = new HomogeneousMatrix();
        double[][] m = matrix.getMatrix();

        m[0][0] = scaleX;
        m[1][1] = scaleY;
        m[2][2] = 1;
        m[3][3] = 1;

        return matrix;
    }
}
class RectangleTransformation {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Rectangle Transformation");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);

                    Graphics2D g2d = (Graphics2D) g;

                    // 创建矩形
                    Rectangle rectangle = new Rectangle(100, 100, 200, 100);
                    g2d.setColor(Color.BLUE);
                    g2d.fill(rectangle);

                    // 创建旋转变换
                    AffineTransform rotate = new AffineTransform();
                    rotate.rotate(Math.toRadians(45), 200, 200);

                    // 创建缩放变换
                    AffineTransform scale = new AffineTransform();
                    scale.scale(3,3);
                    scale.translate(200, 200);

                    // 应用变换到矩形
                    g2d.setTransform(rotate);
                    g2d.transform(scale);

                    g2d.setColor(Color.RED);
                    g2d.fill(rectangle);
                }
            };

            frame.add(panel);
            frame.setSize(400, 400);
            frame.setVisible(true);
        });
    }
}
public class Test1 {
    public static void main(String[] args) {
    }

}
